﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace GovernmentOfficeProject.SysTools
{
    /// <summary>
    /// 日期工具类
    /// </summary>
    /// 
    public class DateTimeUtil
    {

        /// <summary>
        /// 日期转字符串类型
        /// </summary>
        /// <param name="date">日期</param>
        /// <param name="formatString">格式化字符串</param>
        /// <returns></returns>
        public static string Date2String(DateTime date, string formatString)
        {
            return date.ToString(formatString);
        }


        /// <summary>
        /// 字符串转日期类型
        /// </summary>
        /// <param name="dateStr"></param>
        /// <returns></returns>
        public static DateTime String2Date(string dateStr)
        {
            return DateTime.Parse(dateStr);
        }

        /// <summary>
        /// 格式化日期
        /// </summary>
        /// <param name="date">日期</param>
        /// <param name="formatString">格式化字符串</param>
        /// <returns></returns>
        public static DateTime FormatDate(DateTime date,string formatString)
        {
            return Convert.ToDateTime(date.ToString(formatString));
        }

        /// <summary>
        /// 获得月的第一天
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns></returns>
        public static DateTime GetFirstDayForMonth(DateTime date)
        {
            DateTime firstDay = FormatDate(date,"yyyy-MM-1 HH:mm:ss");
            return firstDay;
        }

        /// <summary>
        /// 获得月的最后一天
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns></returns>
        public static DateTime GetLastDayForMonth(DateTime date)
        {
            DateTime firstDay = GetFirstDayForMonth(date);
            return FormatDate(firstDay,"yyyy-MM-dd").AddMonths(1).AddSeconds(-1);
        }


        /// <summary>
        /// 获得本日期的周一日期
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime GetThisDateMonday(DateTime date)
        {
            DateTime first = System.DateTime.Now;
            switch (date.DayOfWeek)
            {
                case System.DayOfWeek.Monday:
                    first = date;
                    break;
                case System.DayOfWeek.Tuesday:
                    first = date.AddDays(-1);
                    break;
                case System.DayOfWeek.Wednesday:
                    first = date.AddDays(-2);
                    break;
                case System.DayOfWeek.Thursday:
                    first = date.AddDays(-3);
                    break;
                case System.DayOfWeek.Friday:
                    first = date.AddDays(-4);
                    break;
                case System.DayOfWeek.Saturday:
                    first = date.AddDays(-5);
                    break;
                case System.DayOfWeek.Sunday:
                    first = date.AddDays(-6);
                    break;
            }
            return first;
        }
    }
}
